using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using System.IO.Ports;
using System.Text;

#if MINI
using SecretLabs.NETMF.Hardware.NetduinoMini;
#else
using SecretLabs.NETMF.Hardware.Netduino;
#endif

namespace codebase
{
    public class windowInput : UI.Window
    {
        UI.Button[] inputNumberKeys;
        UI.Button[] inputAlphaKeys;
        UI.Frame alphaFrame;
        UI.Button inputOK;
        UI.Button inputCancel;
        public UI.Label inputResult;
        UI.Button inputDel;
        UI.Button inputDelAll;
        Settings.Inputs setting;

        SerialMonitor mon;

        public windowInput(UI.WindowManager manager, SerialMonitor monitor)
        {
            Initialize(manager, monitor);
        }

        public void Initialize(UI.WindowManager manager, SerialMonitor monitor)
        {
            base.Initialize(manager, 26, 21);

            mon = monitor;

            Left = 4 * Parent.screen.CharacterWidth;
            Top = 14 * Parent.screen.CharacterHeight;

            FullScreen = false;
            Text = "Input";

            inputNumberKeys = new UI.Button[14];
            inputNumberKeys[0] = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 2);
            inputNumberKeys[0].Text = "\n 7 \n";
            inputNumberKeys[1] = (UI.Button)Add(UI.Window.ControlTypes.Button, 4, 2);
            inputNumberKeys[1].Text = "\n 8 \n";
            inputNumberKeys[2] = (UI.Button)Add(UI.Window.ControlTypes.Button, 7, 2);
            inputNumberKeys[2].Text = "\n 9 \n";

            inputNumberKeys[3] = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 5);
            inputNumberKeys[3].Text = "\n 4 \n";
            inputNumberKeys[4] = (UI.Button)Add(UI.Window.ControlTypes.Button, 4, 5);
            inputNumberKeys[4].Text = "\n 5 \n";
            inputNumberKeys[5] = (UI.Button)Add(UI.Window.ControlTypes.Button, 7, 5);
            inputNumberKeys[5].Text = "\n 6 \n";

            inputNumberKeys[6] = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 8);
            inputNumberKeys[6].Text = "\n 1 \n";
            inputNumberKeys[7] = (UI.Button)Add(UI.Window.ControlTypes.Button, 4, 8);
            inputNumberKeys[7].Text = "\n 2 \n";
            inputNumberKeys[8] = (UI.Button)Add(UI.Window.ControlTypes.Button, 7, 8);
            inputNumberKeys[8].Text = "\n 3 \n";

            inputNumberKeys[9] = (UI.Button)Add(UI.Window.ControlTypes.Button, 10, 8);
            inputNumberKeys[9].Text = "\n 0 \n";

            inputNumberKeys[10] = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 11);
            inputNumberKeys[10].Text = "\n [CR] \n";

            inputNumberKeys[11] = (UI.Button)Add(UI.Window.ControlTypes.Button, 7, 11);
            inputNumberKeys[11].Text = "\n [LF] \n";

            inputNumberKeys[12] = (UI.Button)Add(UI.Window.ControlTypes.Button, 11, 17);
            inputNumberKeys[12].Text = "\n [DEC] \n";

            inputNumberKeys[13] = (UI.Button)Add(UI.Window.ControlTypes.Button, 18, 17);
            inputNumberKeys[13].Text = "\n [HEX] \n";

            inputDel = (UI.Button)Add(UI.Window.ControlTypes.Button, 10, 2);
            inputDel.Text = "\n < \n";
            inputDel.Pressed += new UI.Button.ButtonPressedDelegate(inputNumberDel_Pressed);

            inputDelAll = (UI.Button)Add(UI.Window.ControlTypes.Button, 10, 5);
            inputDelAll.Text = "\n X \n";
            inputDelAll.Pressed += new UI.Button.ButtonPressedDelegate(inputNumberDelAll_Pressed);

            alphaFrame = (UI.Frame)Add(UI.Window.ControlTypes.Frame, 15, 2);
            alphaFrame.Width = cX(10);
            alphaFrame.Height = cY(12);
            alphaFrame.Text = "Characters";

            inputAlphaKeys = new UI.Button[84];

            int keycount = 0;
            int startx = 15;
            int starty = 3;

            int colcount = 0;
            int rowcount = 0;

            for (int key = 33; key <= 126; key++)
            {
                if (key < 48 || key > 57)
                {
                    char _key = (char)key;
                    inputAlphaKeys[keycount] = (UI.Button)Add(UI.Window.ControlTypes.Button, startx + colcount, starty + rowcount);
                    inputAlphaKeys[keycount].Text = _key.ToString();
                    inputAlphaKeys[keycount].Released += new UI.Button.ButtonReleasedDelegate(inputN_Pressed);
                    inputAlphaKeys[keycount].Padding = 0;
                    inputAlphaKeys[keycount].ForeColor = ColorExtensions.WHITE16;

                    colcount++;
                    if (colcount >= 10 || key == 64 || key == 90 || key == 96 || key == 122)
                    {
                        colcount = 0;
                        rowcount++;
                    }

                    keycount++;
                }
            }

            inputResult = (UI.Label)Add(UI.Window.ControlTypes.Label, 1, 15);
            inputResult.Text = "";

            inputOK = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 17);
            inputOK.Text = "\n OK \n";
            inputOK.Released += new UI.Button.ButtonReleasedDelegate(inputNumberOK_Released);

            inputCancel = (UI.Button)Add(UI.Window.ControlTypes.Button, 5, 17);
            inputCancel.Text = "\n End \n";
            inputCancel.Released += new UI.Button.ButtonReleasedDelegate(inputNumberCancel_Released);

            foreach (UI.Button btn in inputNumberKeys)
            {
                btn.Pressed += new UI.Button.ButtonPressedDelegate(inputN_Pressed);
                btn.NotPressedColor = ColorExtensions.RGB16(60, 220, 60);
                btn.PressedColor = ColorExtensions.RGB16(60, 60, 220);
            }

            inputNumberKeys[11].NotPressedColor = ColorExtensions.RGB16(90, 110, 180);
            inputNumberKeys[11].ForeColor = ColorExtensions.WHITE16;
            inputNumberKeys[10].NotPressedColor = ColorExtensions.RGB16(90, 110, 180);
            inputNumberKeys[10].ForeColor = ColorExtensions.WHITE16;
        }

        void inputNumberCancel_Released(UI.Button btn)
        {
            Parent.inFront = null;

            Parent.Remove(this);

            Parent.Redraw();
        }

        void inputNumberOK_Released(UI.Button btn)
        {
            if (inputResult.Text.Length > 0)
            {
                if (setting == Settings.Inputs.SetAnswerLength)
                {
                    int number = 0;

                    try
                    {
                        number = Convert.ToInt32(inputResult.Text);
                    }
                    catch
                    {
                        return;
                    }

                    mon.settings.answerLength = number;
                }
                else if (setting == Settings.Inputs.SetTimeOutInterval)
                {
                    int number = 0;

                    try
                    {
                        number = Convert.ToInt32(inputResult.Text);
                    }
                    catch
                    {
                        return;
                    }
                    
                    mon.settings.timeOutInterval = number;
                }
                else if (setting == Settings.Inputs.Send)
                {
                    string val = inputResult.Text;
                    mon.terminal.Send(val);
                    mon.settings.lastSendText = val;
                }
            }
        }

        void inputNumberDel_Pressed(UI.Button btn)
        {
            string txt = inputResult.Text;
            inputResult.Clear(txt.Length);
            if (txt.Length >= 1) txt = txt.Substring(0, txt.Length - 1);
            inputResult.Text = txt;
            inputResult.Refresh();
        }

        void inputNumberDelAll_Pressed(UI.Button btn)
        {
            string txt = inputResult.Text;
            inputResult.Clear(txt.Length);
            txt = "";
            inputResult.Text = txt;
            inputResult.Refresh();
        }

        void inputN_Pressed(UI.Button btn)
        {
            string txt = btn.Text.Replace("\n", "");
            txt = txt.Replace(" ", "");
            inputResult.Text += txt;
            inputResult.Refresh();
        }

        public void Show(Settings.Inputs setting, string input, bool alpha)
        {
            input = input.Replace("\r", "[CR]");
            input = input.Replace("\n", "[LF]");

            if (alpha)
            {
                Left = 4 * Parent.screen.CharacterWidth;
                Width = 26 * Parent.screen.CharacterWidth;

                foreach (UI.Button b in inputAlphaKeys)
                {
                    b.Visible = true;
                }

                inputNumberKeys[10].Visible = true;
                inputNumberKeys[11].Visible = true;
                inputNumberKeys[12].Visible = true;
                inputNumberKeys[13].Visible = true;

                alphaFrame.Visible = true;
            }
            else
            {
                Left = 10 * Parent.screen.CharacterWidth;
                Width = 14 * Parent.screen.CharacterWidth;

                foreach (UI.Button b in inputAlphaKeys)
                {
                    b.Visible = false;
                }

                inputNumberKeys[10].Visible = false;
                inputNumberKeys[11].Visible = false;
                inputNumberKeys[12].Visible = false;
                inputNumberKeys[13].Visible = false;

                alphaFrame.Visible = false;
            }

            this.setting = setting;
            Parent.inFront = this;

            inputResult.Text = input;

            Visible = true;
            Redraw();
        }
    }
}
